<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\AuthGroupAccessApi;
use Admin\Api\AuthGroupApi;
use Admin\Api\MemberApi;
use Admin\Api\VUserInfoApi;
use Common\Api\BoyeServiceApi;
use Think\Exception;
use Uclient\Api\UserApi;
use Shop\Api\MemberConfigApi;

class MemberController extends AdminController {
    /**
	 * 审核失败
	 */
	public function fail(){
		$map=array('uid'=>I('id',0));
		$entity=array('identity_validate'=>0);
		$result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
		if($result['status']){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
    /**
	 * 删除用户
	 * 假删除
	 */
	public function delete(){
		if(is_administrator(I('uid',0))){
			$this->error("禁止对超级管理员进行删除操作！");
		}
		parent::pretendDelete("uid");
	}
    /**
	 * 启用
	 */
	public function enable(){
		parent::enable("uid");
	}
    /**
	 * 禁用
	 */
	public function disable(){
		if(is_administrator(I('uid',0))){
			$this->error("禁止对超级管理员进行禁用操作！");
		}
		parent::disable("uid");
	}

	private function register($username = '', $password = '',$email){
        $url = "";
        $data = [
            'type'     => 'By_User_register',
            'api_ver'  => '101',
            'notify_id'=> time(),
            'nickname' => $username,
            'username' => $username,
            'password' => $password,
            'country'  => "+86",
            'code'     => "itboye",
            'reg_from'=> 0,
            'alg'=>'md5',
        ];
        $result = (new BoyeServiceApi())->callRemote($url, $data);
        return $result;
    }

    /**
	 * add
	 */
    public function add($username = '', $password = '', $repassword = '', $email = ''){

		if(IS_POST){
			;
			if($password != $repassword){
				$this->error("密码和重复密码不一致！");
			}
            $result = $this->register($username, $password, $email);

            if(!$result['status']){
                $this->error('用户添加失败！'.$result['info']);
            }
            $this->success('用户添加成功！',U('Member/index'));

		}else{
			$this->display();
		}
	}

    /**
	 * 检测用户名是否已存在
	 */
	public function check_username($username){
		$result = apiCall(UserApi::CHECK_USER_NAME,array($username));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}

    /**
	 * 检测用户名是否已存在
	 */
	public function check_email(){
			$result = apiCall(UserApi::CHECK_EMAIL,array($email));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}

    /**
	 *
	 */
	public function select(){

		$map['nickname'] = array('like', "%" . I('q', '', 'trim') . "%");
		$map['uid'] = I('q',-1);
		$map['_logic'] = 'OR';
		$page = array('curpage'=>0,'size'=>20);
		//$order = " last_login_time desc ";

		$result = apiCall(MemberApi::QUERY, array($map,$page, $order,false,'uid,nickname,head'));

		if($result['status']){
			$list = $result['info']['list'];
			foreach($list as $key=>$g){
				$list[$key]['id']=$list[$key]['uid'];
				$list[$key]['head'] = getImageUrl($list[$key]['head'],0,1);
			}

			$this->success($list);
		}

	}

    public function view(){
        $id = I('get.id',0);

        $result = apiCall(MemberApi::GET_INFO, array(array("uid"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userinfo",$result['info']);

        $result = apiCall(UserApi::GET_INFO, array($id));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("useraccount",$result['info']);

        $result = apiCall(AuthGroupAccessApi::QUERY_GROUP_INFO, array($id));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userroles",$result['info']);
        $this->display();
    }


    public function index() {
        $params = array();
        $u_group = I('post.u_group','','trim');
        $nickname = I('nickname','','trim');
        if(!empty($nickname)){
            $map['id|mobile'] = $nickname;
            $map['nickname'] = array('like', "%" . $nickname . "%");
            $map['_logic'] = 'OR';
            $where['_complex'] = $map;
        }
        if(!empty($u_group)){
            $where['u_group'] = $u_group;
        }

//        `nickname` LIKE '%%' OR `uid` = '' OR `mobile` = ''
        $page = array('curpage' => I('get.p'), 'size' => C('LIST_ROW'));
        $order = " update_time desc ";
        $params['nickname'] = $nickname;
        $result = (new VUserInfoApi())->query($where, $page, $order);
        if ($result['status']) {

            $this -> assign("nickname", $nickname);
            $this -> assign("show", $result['info']['show']);
            $this -> assign("list", $result['info']['list']);

            $result = (new AuthGroupApi())->queryNoPaging([]);
            $this->assign('u_group',$u_group);
            $this->assign('user_group',$result['info']);
            $this -> display();
        } else {
            $this -> error($result['info']);
        }
    }

    /**
     * 实名审核
     */
    public function realname(){
        $uid = I('uid',0,'int');
        $this->assign('uid',$uid);
        if($uid >0 ){
            $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
            if(!$r['status']) $this->error($r['info']);
            if(empty($r['info'])) $this->error('uid错误');
            $nickname = $r['info']['nickname'];
        }else{
            $nickname = '';
        }
        $this->assign('nickname',$nickname);
        $page = array('curpage' => I('get.p'), 'size' => 15);
        $map  = array('identity_validate'=>2);
        $result = apiCall(MemberConfigApi::QUERY,array($map,$page));
        $this->assign('member',$result['info']['list']);
        // $resu = apiCall(MemberApi::QUERY_NO_PAGING,array());
        // $this->assign('user',$resu['info']);
        $this -> display();
    }
    /**
     * 审核通过
     */
    public function pass(){
        $map=array('uid'=>I('id',0));
        $entity=array('identity_validate'=>1);
        $result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
        if($result['status']){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
}
